package org.yate.oa.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yate.oa.model.Blueprint;
import org.yate.oa.model.FtpContent;
import org.yate.oa.model.HighVoltage;
import org.yate.oa.model.LowBCKZQ;
import org.yate.oa.model.LowBJ;
import org.yate.oa.model.LowBLQ;
import org.yate.oa.model.LowDLHGQ;
import org.yate.oa.model.LowDLQ;
import org.yate.oa.model.LowDRQ;
import org.yate.oa.model.LowGLKG;
import org.yate.oa.model.LowGLKGRDQ;
import org.yate.oa.model.LowJLJCQ;
import org.yate.oa.model.LowZLDRQ;
import org.yate.oa.model.Transformer;
import org.yate.oa.result.AjaxResult;
import org.yate.oa.service.ApiService;
import org.yate.oa.service.FtpContentService;
import org.yate.oa.view.CommonParm;
import org.yate.oa.view.HighVoltageParm;
import org.yate.oa.view.LowPram;
import org.yate.oa.view.ResultData;
import org.yate.oa.view.ResultLow;
import org.yate.oa.view.TransformerParm;

import com.alibaba.fastjson.JSON;



@Controller
@RequestMapping("/api")
public class ApiController {
	
	@Resource
	private ApiService apiService;
	
	@Resource
	private FtpContentService ftpContentService;
	
	 final static String ele[]={"40A",  "32A", "63A", "50A", "80A", "100A" ,"125A" ,"160A" ,"250A", "200A" ,"315A", "400A" ,"630A"};
	 
	 final static String kva[]={"50","80","100","125","160","200","250","315","400","500","630","800","1000","其他"};
	
	@RequestMapping("/getAllData")
	@ResponseBody
    public AjaxResult getAllData(CommonParm commonParm,HighVoltageParm highVoltageParm,TransformerParm transformerParm,
    		String dlqParm,String bjParm,String blqParm,String dlhgqParm,String jljcqParm,String glkgParm,
    		String drqParm,String bckzqParm,String glkgrdqParm,String zldrqParm,HttpServletRequest request){
		ResultData r = new ResultData();
		List<ResultLow> lowList = new ArrayList<ResultLow>();
		LowPram lowPram = new LowPram();
		List<LowBJ> bjParmlist = JSON.parseArray(bjParm, LowBJ.class);
		List<LowDLQ> dlqParmlist = JSON.parseArray(dlqParm.toString(), LowDLQ.class);
		List<LowBLQ> blqParmlist = JSON.parseArray(blqParm, LowBLQ.class);
		List<LowDLHGQ> dlhgqParmlist = JSON.parseArray(dlhgqParm, LowDLHGQ.class);
		List<LowJLJCQ> jljcqParmlist = JSON.parseArray(jljcqParm, LowJLJCQ.class);
		List<LowGLKG> glkgParmlist = JSON.parseArray(glkgParm, LowGLKG.class);
		List<LowDRQ> drqParmlist = JSON.parseArray(drqParm, LowDRQ.class);
		List<LowBCKZQ> bckzqParmlist = JSON.parseArray(bckzqParm, LowBCKZQ.class);
		List<LowGLKGRDQ> glkgrdqParmlist = JSON.parseArray(glkgrdqParm, LowGLKGRDQ.class);
		List<LowZLDRQ> zldrqParmlist = JSON.parseArray(zldrqParm, LowZLDRQ.class);
		lowPram.setBjParm(bjParmlist);
		lowPram.setBckzqParm(bckzqParmlist);
		lowPram.setBlqParm(blqParmlist);
		lowPram.setDlhgqParm(dlhgqParmlist);
		lowPram.setDlqParm(dlqParmlist);
		lowPram.setDrqParm(drqParmlist);
		lowPram.setGlkgParm(glkgParmlist);
		lowPram.setGlkgrdqParm(glkgrdqParmlist);
		lowPram.setJljcqParm(jljcqParmlist);
		lowPram.setZldrqParm(zldrqParmlist);
		
		this.getDLq(commonParm,lowPram,r,lowList);
		this.getBJ(commonParm,lowPram,r,lowList);
		this.getDLHGQ(commonParm,lowPram,r,lowList);
		this.getBLQ(commonParm,lowPram,r,lowList);
		this.getJLJCQ(commonParm,lowPram,r,lowList);
		this.getGLKG(commonParm,lowPram,r,lowList);
		this.getGLKGDRQ(commonParm,lowPram,r,lowList);
		this.getDRQ(commonParm,lowPram,r,lowList);
		this.getBCKZQ(commonParm,lowPram,r,lowList);
		this.getZLDRQ(commonParm,lowPram,r,lowList);
		r.setLowList(lowList);
		HighVoltage h = this.getHighVoltage(commonParm, highVoltageParm);
		Transformer t = this.getTransformer(commonParm, transformerParm);
		String gaoyaData = "";
		String bianyaqiData ="";
		if(h!=null){
			gaoyaData = h.getDetail();
		}
		if(t!=null){
			bianyaqiData = t.getDetail();
		}
		r.setBiayaqiData(bianyaqiData);
		r.setGaotaData(gaoyaData);
		Blueprint b  = apiService.getBlueprint();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
		if(b!=null){
			String path = basePath+"static/source/"+String.valueOf(b.getFtpId())+".html";
			r.setUrl(path);
			FtpContent f = ftpContentService.getEntity(b.getFtpId());
			if(f!=null){
				r.setName(f.getValue());
			}
		}
        return AjaxResult.success(r);
    }
	
	
	/**
	 * 获取高压数据
	 * @param commonParm
	 * @param highVoltageParm
	 * @return
	 */
	private HighVoltage getHighVoltage(CommonParm commonParm,HighVoltageParm highVoltageParm){
		HighVoltage highVoltage = new HighVoltage();
		highVoltage.setType(highVoltageParm.getType());
		highVoltage.setKva(commonParm.getKva());
		highVoltage.setBoxNum(commonParm.getBoxNum());
		highVoltage.setHighVol(commonParm.getHighVol());
		highVoltage.setLowVol(commonParm.getLowVol());
		highVoltage.setHighVolConf(commonParm.getHighVolConf());
		//高压开关柜
		if(highVoltageParm.getType()!=null && highVoltageParm.getType()==1){
			highVoltage.setBrand(highVoltageParm.getBrand());
			highVoltage.setOperationCode(highVoltageParm.getOperationCode());
			highVoltage.setHighVolBoxConf(highVoltageParm.getHighVolBoxConf());
		}else if(highVoltageParm.getType()!=null && highVoltageParm.getType()==0){
			highVoltage.setVoltageConf(commonParm.getVoltageConf());
		}
		return apiService.getHighVoltage(highVoltage);
	}
	
	/**
	 * 获取变压器
	 * @param commonParm
	 * @param transformerParm
	 * @return
	 */
	private Transformer getTransformer(CommonParm commonParm,TransformerParm transformerParm){
		Transformer e = new Transformer();
		e.setType(transformerParm.getTrType());
		e.setKva(commonParm.getKva());
		e.setHighVol(commonParm.getHighVol());
		e.setLowVol(commonParm.getLowVol());
		e.setBoxNum(commonParm.getBoxNum());
		e.setHighVolConf(commonParm.getHighVolConf());
		e.setVoltageConf(commonParm.getVoltageConf());
		return apiService.getTransformer(e);
	}
	
	/**
	 * 断路器
	 * @param lowPram
	 * @return
	 */
	private List<String> getDLq(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowDLQ> dlqParm = lowPram.getDlqParm();
		if(dlqParm!=null){
			for (int i = 0; i < dlqParm.size(); i++) {
				LowDLQ e = dlqParm.get(i);
				LowDLQ newLowDLQ = new LowDLQ();
				newLowDLQ.setLowOCConf(e.getLowOCConf());
				newLowDLQ.setBoxNum(commonParm.getBoxNum());
				newLowDLQ.setType(e.getType());
				newLowDLQ.setBrand(e.getBrand());
				newLowDLQ.setTkqType(e.getTkqType());
				newLowDLQ.setElectricCurrent(e.getElectricCurrent());
				newLowDLQ.setKva(commonParm.getKva());
				LowDLQ d  = apiService.getLowDLQ(newLowDLQ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
			}
			for (String s : list) {
				ResultLow rl = new ResultLow();
				rl.setDlq(s);
				lowList.add(rl);
			}
			//r.setLowList(lowList);
		}
		return list;
	}
	
	/**
	 * 表计
	 * @param lowPram
	 * @return
	 */
	private List<String> getBJ(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowBJ> bjParm = lowPram.getBjParm();
		
		if(bjParm!=null){
			for (int i = 0; i < bjParm.size(); i++) {
				LowBJ e = bjParm.get(i);
				LowBJ newLowBJ = new LowBJ();
				newLowBJ.setLowOCConf(e.getLowOCConf());
				newLowBJ.setBoxNum(commonParm.getBoxNum());
				newLowBJ.setType(e.getType());
				LowBJ d  = apiService.getLowBJ(newLowBJ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
			}
			for (int i = 0 ;i<list.size();i++) {
				lowList.get(i).setBj(list.get(i));
			}
		}
		return list;
	}


	/**
	 * 电流互感器
	 * @param lowPram
	 * @return
	 */
	private List<String> getDLHGQ(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowDLHGQ> dlhgqParm = lowPram.getDlhgqParm();
		if( dlhgqParm!=null){
			for (int i = 0; i < dlhgqParm.size(); i++) {
				LowDLHGQ e = dlhgqParm.get(i);
				LowDLHGQ newLowBJ = new LowDLHGQ();
				newLowBJ.setLowOCConf(e.getLowOCConf());
				newLowBJ.setBoxNum(commonParm.getBoxNum());
				newLowBJ.setKva(commonParm.getKva());
				LowDLHGQ d  = apiService.getLowDLHGQ(newLowBJ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
			}
			for (int i = 0 ;i<list.size();i++) {
				lowList.get(i).setDlhgq(list.get(i));
			}
		}
		return list;
	}
	
	/**
	 * 避雷器
	 * @param lowPram
	 * @return
	 */
	private List<String> getBLQ(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowBLQ> blqParm = lowPram.getBlqParm();
		if(blqParm!=null){
			for (int i = 0; i < blqParm.size(); i++) {
				LowBLQ newLowBJ = new LowBLQ();
				newLowBJ.setBoxNum(commonParm.getBoxNum());
				LowBLQ d  = apiService.getLowBLQ(newLowBJ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
			}
			for (int i = 0 ;i<list.size();i++) {
				lowList.get(i).setBlq(list.get(i));
			}
			//r.setLowList(lowList);
		}
		return list;
	}
	
	/**
	 * 交流接触器
	 * @param lowPram
	 * @return
	 */
	private List<String> getJLJCQ(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowJLJCQ> jljcqParm = lowPram.getJljcqParm();
		if(jljcqParm!=null){
			for (int i = 0; i < jljcqParm.size(); i++) {
				LowJLJCQ e = jljcqParm.get(i);
				LowJLJCQ newLowBJ = new LowJLJCQ();
				newLowBJ.setLowOCConf(e.getLowOCConf());
				newLowBJ.setBoxNum(commonParm.getBoxNum());
				newLowBJ.setBrand(e.getBrand());
				newLowBJ.setElectricCurrent(e.getElectricCurrent());
				newLowBJ.setKva(commonParm.getKva());
				LowJLJCQ d  = apiService.getLowJLJCQ(newLowBJ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
			}
			for (int i = 0 ;i<list.size();i++) {
				lowList.get(i).setJljcq(list.get(i));
			}
		}
		return list;
	}
	
	/**
	 * 隔离开关
	 * @param lowPram
	 * @return
	 */
	private List<String> getGLKG(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowGLKG> glkgParm = lowPram.getGlkgParm();
		if(glkgParm!=null){
			for (int i = 0; i < glkgParm.size(); i++) {
				LowGLKG e = glkgParm.get(i);
				LowGLKG newLowBJ = new LowGLKG();
				newLowBJ.setLowOCConf(e.getLowOCConf());
				newLowBJ.setBoxNum(commonParm.getBoxNum());
				newLowBJ.setKva(commonParm.getKva());
				LowGLKG d  = apiService.getLowGLKG(newLowBJ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
			}
			for (int i = 0 ;i<list.size();i++) {
				lowList.get(i).setGlkg(list.get(i));
			}
		}
		return list;
	}
	
	/**
	 * 隔离开关段容器
	 * @param lowPram
	 * @return
	 */
	private List<String> getGLKGDRQ(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowGLKGRDQ> glkgParm = lowPram.getGlkgrdqParm();
		if(glkgParm!=null){
			for (int i = 0; i < glkgParm.size(); i++) {
				LowGLKGRDQ e = glkgParm.get(i);
				LowGLKGRDQ newLowBJ = new LowGLKGRDQ();
				newLowBJ.setLowOCConf(e.getLowOCConf());
				newLowBJ.setBoxNum(commonParm.getBoxNum());
				newLowBJ.setKva(commonParm.getKva());
				LowGLKGRDQ d  = apiService.getLowGLKGRDQ(newLowBJ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
			}
			for (int i = 0 ;i<list.size();i++) {
				lowList.get(i).setGlkgrdq(list.get(i));
			}
		}
		return list;
	}
	
	/**
	 * 电容器
	 * @param lowPram
	 * @return
	 */
	private List<String> getDRQ(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowDRQ> glkgParm = lowPram.getDrqParm();
		if(glkgParm!=null){
			for (int i = 0; i < glkgParm.size(); i++) {
				LowDRQ e = glkgParm.get(i);
				LowDRQ newLowBJ = new LowDRQ();
				newLowBJ.setKva(commonParm.getKva());
				newLowBJ.setBoxNum(e.getBoxNum());
				LowDRQ d  = apiService.getLowDRQ(newLowBJ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
			}
			for (int i = 0 ;i<list.size();i++) {
				lowList.get(i).setDrq(list.get(i));
			}
		}
		return list;
	}
	
	/**
	 * 补偿控制器
	 * @param lowPram
	 * @return
	 */
	private List<String> getBCKZQ(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowBCKZQ> glkgParm = lowPram.getBckzqParm();
		if(glkgParm!=null){
			for (int i = 0; i < glkgParm.size(); i++) {
				LowBCKZQ e = glkgParm.get(i);
				LowBCKZQ newLowBJ = new LowBCKZQ();
				newLowBJ.setBrand(e.getBrand());
				newLowBJ.setBoxNum(commonParm.getBoxNum());
				newLowBJ.setKva(commonParm.getKva());
				LowBCKZQ d  = apiService.getLowBCKZQ(newLowBJ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
			}
			for (int i = 0 ;i<list.size();i++) {
				lowList.get(i).setBckzq(list.get(i));
			}
		}
		return list;
	}
	
	/**
	 * 智能电容器器
	 * @param lowPram
	 * @return
	 */
	private List<String> getZLDRQ(CommonParm commonParm,LowPram lowPram,ResultData r,List<ResultLow> lowList){
		List<String> list = new ArrayList<String>();
		List<LowZLDRQ> glkgParm = lowPram.getZldrqParm();
		if(glkgParm!=null){
				list.add("");
				list.add("");
				LowZLDRQ e = glkgParm.get(2);
				LowZLDRQ newLowBJ = new LowZLDRQ();
				newLowBJ.setBrand(e.getBrand());
				newLowBJ.setKva(commonParm.getKva());
				newLowBJ.setBoxNum(commonParm.getBoxNum());
				LowZLDRQ d  = apiService.getLowZLDRQ(newLowBJ);
				if(d!=null){
					list.add(d.getDetail());
				}else{
					list.add("");
				}
				list.add("");
			for (int i = 0 ;i<list.size();i++) {
				lowList.get(i).setZldrq(list.get(i));
			}
		}
		return list;
	}
	
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		list.add(5);
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i)==2){
				list.remove(i);
			}
		}
		System.out.println(list.size());
	}
}
